package com.example.onion.hxchat.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.onion.hxchat.room.entity.Friend;
import com.example.onion.hxchat.room.entity.TeamNotification;
import com.example.onion.hxchat.room.entity.User;
import com.example.onion.hxchat.test.TestDefineMessage;
import com.example.onion.hxchat.thread.GetUserNameThread;
import com.example.onion.hxchat.thread.PlayUserDetailThread;
import com.example.onion.hxchat.util.HttpUtils;
import com.example.superyi.myteam.R;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * @author:onion
 * @data: 2020/8/21
 */
public class MyTeamNotificationAdapter extends RecyclerView.Adapter<MyTeamNotificationAdapter.ViewHolder> {
    private List<TeamNotification> mList = new ArrayList<>();
    private Context mContext;
    private MyTeamNotificationAdapter.DataGetterListener mDataGetterListener;

    public void setmList(List<TeamNotification> mList) {
        this.mList = mList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CircleImageView userImg;//发送相关队伍消息的用户id
        TextView nameText;//发送相关队伍消息的用户名
        TextView notificationText;//发送相关队伍消息的提示信息
        Button acceptApply;//同意队伍请求
        Button deleteApply;//删除提示队伍删除的信息（按钮显示的是”我知道了“）
        Button rejectApply;//拒绝入队邀请

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            userImg = itemView.findViewById(R.id.team_notification_user_img_civ_wc);
            nameText = itemView.findViewById(R.id.team_notification_user_name_tv_wc);
            notificationText = itemView.findViewById(R.id.team_notification_info_tv_wc);
            acceptApply = itemView.findViewById(R.id.team_notification_accept_apply_btn_wc);
            deleteApply = itemView.findViewById(R.id.team_notification_delete_btn_wc);
            rejectApply = itemView.findViewById(R.id.team_notification_reject_apply_btn_wc);
        }
    }

    public MyTeamNotificationAdapter(Context context, List<TeamNotification> mList) {
        mContext = context;
        this.mList = mList;
    }

    public MyTeamNotificationAdapter(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public MyTeamNotificationAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载子项的布局文件，得到视图，传入ViewHolder,套路写法
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.team_notification_item, parent, false);
        MyTeamNotificationAdapter.ViewHolder holder = new MyTeamNotificationAdapter.ViewHolder(view);
        return holder;
    }
//    @SuppressLint("HandlerLeak")
//    private Handler handler = new Handler() {
//        @Override
//        public void handleMessage(@NonNull Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case GetUserNameThread.GETNAME_IS_FINISH:
//                    String data = (String) msg.obj;
//                    setContent(data);
//                    break;
//                case GetUserNameThread.GETNAME_IS_FAILED:
//                    break;
//                default:
//                    break;
//            }
//        }
//    };
    @Override
    public void onBindViewHolder(@NonNull MyTeamNotificationAdapter.ViewHolder holder, final int position) {
        TeamNotification teamNotification = mList.get(position);
        holder.userImg.setImageResource(R.drawable.user);//这里根据url请求用户头像
//        String userName = getUserNameById(teamNotification.getUserId());
        holder.nameText.setText(teamNotification.getUserName());
//        new Thread(new GetUserNameThread(teamNotification.userId,handler)).start();
        holder.notificationText.setText(teamNotification.getTeamNotificationInfo());
        Log.e("TeamNotification type",teamNotification.getTeamNotificationType());
        if (TestDefineMessage.TEAM_NOTIFICATION_INVITE.equals(teamNotification.getTeamNotificationType())){
            //邀请入队的消息，应该显示两个接受和拒绝的按钮
            holder.deleteApply.setVisibility(View.GONE);//隐藏"我知道了"的按钮，显示接受和拒绝的按钮
//            holder.acceptApply.setVisibility(View.VISIBLE);
//            holder.deleteApply.setVisibility(View.VISIBLE);
        }else {
            //自然就是删除队伍的提示消息，只显示我知道了按钮
//            holder.deleteApply.setVisibility(View.VISIBLE);
            holder.acceptApply.setVisibility(View.GONE);
            holder.deleteApply.setVisibility(View.GONE);
        }
//        CircleImageView userImg;//发送相关队伍消息的用户id
//        TextView nameText;//发送相关队伍消息的用户名
//        TextView notificationText;//发送相关队伍消息的提示信息
//        Button acceptApply;//同意队伍请求
//        Button deleteApply;//删除提示队伍删除的信息（按钮显示的是”我知道了“）
//        Button rejectApply;//拒绝入队邀请

        holder.acceptApply.setOnClickListener(new View.OnClickListener() {//同意入队请求
            @Override
            public void onClick(View v) {
                mDataGetterListener.getItemData(teamNotification);
            }
        });
        holder.deleteApply.setOnClickListener(new View.OnClickListener() {//删除通知（已读）
            @Override
            public void onClick(View v) {
                mDataGetterListener.getItemData(teamNotification);
            }
        });
        holder.rejectApply.setOnClickListener(new View.OnClickListener() {//拒绝入队
            @Override
            public void onClick(View v) {
                mDataGetterListener.getItemData(teamNotification);
            }
        });
    }
    private  boolean isSuccess = false;

//    private String getUserNameById(String id) {
//        final String[] userName = new String[1];
//        HttpUtils httpUtils = HttpUtils.getInstance();
//        FormBody.Builder builder = new FormBody.Builder();
//        builder.add("userId", id);
//        FormBody formBody = builder.build();
//        httpUtils.asyncPost(" http://49.232.60.155:8080/AppServer_war/GetUserNameByIdServlet", formBody, new HttpUtils.HttpCallBack() {
//            @Override
//            public void onError(Request request, IOException e) {
//
//            }
//
//            @Override
//            public void onSuccess(Request request, String result) {
//                Gson gson = new Gson();
//                JsonObject resultJson = gson.fromJson(result, JsonObject.class);
//                JsonElement jsonElement = resultJson.get("userName");
//                userName[0] = jsonElement.getAsString();
//                Log.e("MyTeamAdapter", userName[0]);
//                isSuccess = true;
//            }
//        });
//        while(!isSuccess){
//
//        }
//        isSuccess = false;
//        return userName[0]!=null?userName[0]:id;
//    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public interface DataGetterListener {//适配器内创建一个回调接口，获取每一行recyclerview的相关数据

        void getItemData(TeamNotification friendBean);//将本页面的获得的infoBean数据通过接口回调给调用方
    }

    public void setDataGetterListener(MyTeamNotificationAdapter.DataGetterListener dataGetterListener) {
        this.mDataGetterListener = dataGetterListener;
    }

}
